package com.example.bottomnavigationdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.example.bottomnavigationdemo.ui.a_home.HomeFragment
import com.example.bottomnavigationdemo.ui.b_dashboard.DashboardFragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlin.math.log

/**
 * https://github.com/STAR-ZERO/navigation-keep-fragment-sample fragment的复用
 * https://www.jianshu.com/p/37b066d1e8d2
 * https://blog.csdn.net/m0_46958584/article/details/105430445
 * https://my.oschina.net/u/3732258/blog/4714880   //重用
 * https://blog.csdn.net/weixin_42575043/article/details/108709467?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control
 *   https://www.jianshu.com/p/7c3f7a4e95e3 点跳转闪的问题。 也是重用
 */
class MainActivity : AppCompatActivity() {
    lateinit var navController: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val bottomNavigationView: BottomNavigationView = findViewById(R.id.nav_view)

        /**
         * 一般我们不需要，把主题设置成NoActionBar ,自己添加appbar
         */
//      val appBarConfiguration = AppBarConfiguration(bottomNavigationView.menu)
//      setupActionBarWithNavController(navController, appBarConfiguration)//这个是使用切换的时候，标题随着Navigation里面的Lable更改


        navController = findNavController(R.id.nav_host_fragment)
        bottomNavigationView.setupWithNavController(navController) //使bottomNavigationView与NavController相结合，这样点击了bottomNavigation才会切换界面
        navController.addOnDestinationChangedListener { controller, destination, arguments ->
//            controller.popBackStack()   //如果每次调用这个， 或者在xml里面app:defaultNavHost="true"去掉这句话，点击返回键就会一次退出了。


            when (destination.id) {
                R.id.navigation_home -> {
//                    Log.e("mainactivity--->", "navigation_home")
                }
                R.id.navigation_dashboard -> {
//                    Log.e("mainactivity--->", "navigation_dashboard")
                }
                R.id.navigation_notifications -> {
//                    Log.e("mainactivity--->", "navigation_notifications")
                }
                else -> {
//                    Log.e("mainactivity--->", "关闭！")
                }
            }
        }

        //为了点击底部按钮重创
        bottomNavigationView.setOnNavigationItemSelectedListener {
            // 避免再次点击重复创建
            if (it.isChecked) {
                return@setOnNavigationItemSelectedListener true
            }
            // 避免B返回到A重复创建
            val popBackStack = navController.popBackStack(it.itemId, false)

            if (popBackStack) {
                // 已创建
                return@setOnNavigationItemSelectedListener popBackStack
            } else {
                // 未创建
                return@setOnNavigationItemSelectedListener NavigationUI.onNavDestinationSelected(
                    it, navController
                )
            }
        }
    }

    //就是toolbar左则的那个
    override fun onSupportNavigateUp(): Boolean {
        Log.e("mainactivity--->", "接收导航按钮！！")
        return navController?.navigateUp()

    }

    @SuppressLint("RestrictedApi")
    override fun onBackPressed() {
        //为了防止点击返回键无法退出界面而是退回到切换前的fragment,我们需要处理一下返回键的点击事件:
 /*       if (!navController.popBackStack()) {
            finish()
        } else
            super.onBackPressed()*/
        super.onBackPressed()
//        val backStack = navController.backStack
//        Log.e("mainactivity--->", "关闭！------>onBackPressed（）-" + backStack.size)
    }
//调用fragment
    fun chuanZhi() {
        val fragment = supportFragmentManager?.fragments[0]?.childFragmentManager?.fragments?.get(0)
        if (fragment is HomeFragment) {
            fragment.xx("我是MainActivity")
        }
    }
}